#include <SoftwareSerial.h>

#include <address.h>
#include <adk.h>
#include <avrpins.h>
#include <cdcacm.h>
#include <cdcftdi.h>
#include <cdcprolific.h>
#include <confdescparser.h>
#include <hexdump.h>
#include <hid.h>
#include <hidboot.h>
#include <hidescriptorparser.h>
#include <hiduniversal.h>
#include <hidusagestr.h>
#include <max3421e.h>
#include <max_LCD.h>
#include <message.h>
#include <parsetools.h>
#include <printhex.h>
#include <PS3BT.h>
#include <Usb.h>
#include <usbhost.h>
#include <usbhub.h>
#include <usb_ch9.h>
#include <EEPROM.h>

/*#include <SoftwareSerial.h>
 #include <Usb.h>
 #include <adk.h>
 */
#include "global.h"


SoftwareSerial LCD(0,SOFTWARESERIAL_LCD_OUTPUT);
// Setup Android ADK and USB sheild
USB Usb;


ADK adk(&Usb,"Brew Coder",
"KegController",
"Keg Controller Board",
"1.0",
"www.brewcoder.com",
"0000000000000001");

void setup(){
  // init Inputs and Outputs
  init_PINS();
  init_SoftSerial();


  // init Android ADK Serial port            
  Serial.begin(115200);
  Serial.println("\r\nADK demo start");
  if (Usb.Init() == -1) {
    Serial.println("OSCOKIRQ failed to assert");
    while(1) {
      //   updateDisplay();
      LCD.print("USB Init Failed");
    }
  }

  // Check if EEprom is valid
  if(!DEBUG){
    EEpromCompressorState = EEPROM.read(0);
    if(EEpromCompressorState > 1){
      EEPROM.write(0,0);
      EEpromCompressorState = 0;
      Serial.println("EEPROM Init");
    }

  }
  else{
    Serial.println("Debug Mode");
    EEpromCompressorState = 0;
  }
  Serial.print("EEPROM Setup Read: ");
  Serial.println(EEpromCompressorState);

}

void loop(){

  Usb.Task();
  sampleAllTemps();
  tempControl();
  updateDisplay(); 
  pwmStuff(false);

  if( !adk.isReady()) {
    adkConnected = false;
  }

  else{
    adkConnected = true;
    readMessage();
    transCurrentStatus();    
  }


}

void readMessage(){
  uint8_t rcode;
  uint8_t msg[4] = {
    0x00    }; 
  uint16_t len = sizeof(msg);
  rcode = adk.RcvData(&len, msg);
  //  Serial.print("rcode: ");
  // Serial.println(rcode);

  if(len > 0) {
    switch( msg[0] ) {
    case 2:
 //     Serial.println("\nCase 2 received");
      fridgeSetTemp1 =(long)(msg[2])+(msg[1]<<8);
      //    updateDisplay();
      break;
    case 3:
//      Serial.println("\nCase 3 received");
      if(msg[1] == 0x01){
        if(fanSpeed <= 250)
        fanSpeed = fanSpeed + 5;
      }
      else{
        if(fanSpeed >= 5)
        fanSpeed = fanSpeed - 5;
      }
      break;
    }//switch( msg[1]...  
  }//if( len > 0...    
  //  delay(10);
}
void transCurrentStatus(){  
  byte b;
  uint8_t msgStatus[7] = {
    0x00  };

  msgStatus[0] = 0x03;
  msgStatus[1] = compressorON; 

  int TXfridgeTemp1 = (int)(fridgeTemp1);
  msgStatus[2] =  TXfridgeTemp1 >> 8;
  msgStatus[3] =  TXfridgeTemp1 & 0xff;

  int TXfridgeSetTemp = (int)fridgeSetTemp1;
  msgStatus[4] =  TXfridgeSetTemp >> 8;
  msgStatus[5] =  TXfridgeSetTemp & 0xff;

  msgStatus[6] = pwmRate;
  adk.SndData( 7, msgStatus );

}


void init_PINS(){  
  // Init PWM outputs
  pinMode (LIGHT_LED_FRIDGE,OUTPUT);
  pinMode(AIR_FAN_FRIDGE, OUTPUT);
  pinMode(TEMP_CONTROL_HEAT, OUTPUT);
  pinMode(TEMP_CONTROL_COOL, OUTPUT);
  pinMode(LIGHT_SWITCH_DOOR, INPUT);
  pinMode(TEMP_INPUT_FRIDGE, INPUT);
  pinMode(WEIGHT_SENSOR_KEG1,INPUT);

  analogWrite(LIGHT_LED_FRIDGE,0);
  digitalWrite(AIR_FAN_FRIDGE,0);
  digitalWrite(TEMP_CONTROL_HEAT,0);
  digitalWrite(TEMP_CONTROL_COOL,0);

}

void pwmStuff(boolean fadeOnOff){

  if(fadeOnOff){
    if(millis() - prevMillis_LightUpdate >= lightRefreshRate){
      //analogWrite(LIGHT_LED_FRIDGE, 100);
      if(pwmRate <= 250 && countUp){
        pwmRate += 1;
        analogWrite(LIGHT_LED_FRIDGE, pwmRate);

      }

      if(pwmRate >=250 || !countUp){
        countUp = false;
        pwmRate -= 1;
        analogWrite(LIGHT_LED_FRIDGE, pwmRate);

      }

      if(pwmRate <= 0){
        countUp = true;
        pwmRate += 1;
        analogWrite(LIGHT_LED_FRIDGE, pwmRate);
      }
      prevMillis_LightUpdate = millis();
    }
  }
  else{
    analogWrite(LIGHT_LED_FRIDGE, fanSpeed);
  }
}



